﻿@using Kobets.Automation.Chimera.Common
@using Kobets.Automation.Chimera.Orchestrator.Models

<script type="text/javascript">
    @if (!String.IsNullOrWhiteSpace(ViewBag.TestID)) {
        <text>var tid = @Html.Raw(ViewBag.TestID);</text>
    }
    else
    {
        <text>var tid = 0;</text>
    }
    
    $(window).resize(function () {
        if (window.tid != 0)
            ResizeGrid('#TestGrid');
        else
            ResizeGrid('#TopLevelGrid');
    });
    
    $(window).load(function () {
        var firstLoad = true;
        
        if (window.tid != 0) {
            $('#TestGrid').data('kendoGrid').dataSource.filter(@(Html.Raw(ViewBag.TestID)));
            ResizeGrid('#TestGrid');
        } else {
          ResizeGrid('#TopLevelGrid');
        }
    });
</script>

<script id="testDetailsTemplate" type="text/kendo-tmpl">
@(Html.Kendo().Grid<TestResults>()
            .Name("TestDetailedResults_#=TestID#")
            .Columns(columns =>
                        {
                            columns.Bound(o => o.Result);
                            columns.Bound(o => o.StepStatus).Width(90);
                        })
            .DataSource(dataSource => dataSource
                        .Ajax()
                        .Read(read => read.Action("GetDetailedData", "Results", new { testID = "#=TestID#" }))
                        .PageSize(100)
            )
            .Pageable(x => x.PageSizes(new[] { 10, 20, 50, 100, 500 }))
            .Sortable()
            .Filterable()
            .Events(e => e.DataBound("SetStatusColorDetails"))
            .ToClientTemplate()
)
</script>

<script id="testTemplate" type="text/kendo-tmpl">
@(Html.Kendo().Grid<Tests>()
           .Name("Grid_#=ProductName#_#=Version#")
           .ClientDetailTemplateId("testDetailsTemplate")
           .Columns(columns =>
                        {
                            columns.Bound(p => p.TestID).Hidden(true);
                            columns.Bound(p => p.TestName);
                            columns.Bound(p => p.StartTime).Format("{0:dd-MM-yyyy}");
                            columns.Bound(p => p.EndTime).Format("{0:dd-MM-yyyy}");
                            columns.Bound(p => p.Status);
                            columns.Bound(p => p.MachineName).Title("Machine");
                        })
           .Sortable()
           .Pageable(x => x.PageSizes(new[] { 10, 20, 50, 100, 500 }))
           .Scrollable()
           .Filterable()
           .DataSource(dataSource => dataSource
                        .Ajax()
                        .Sort(s => s.Add(c => c.StartTime).Descending())
                        .Read(read => read.Action("GetData", "Results", new { productName = "#=ProductName#", version = "#=Version#" }))
                        .PageSize(50)
           )
           .Events(ev => ev.DataBound("SetStatusColor"))
           .ToClientTemplate()
      )

</script>

<script id="secondLevelTemplate" type="text/kendo-tmpl">
@(Html.Kendo().Grid<VersionModel>()
           .Name("SecondLevelGrid_#=ProductName#")
           .ClientDetailTemplateId("testTemplate")
           .Columns(columns =>
                        {
                            columns.Bound(p => p.ProductName).Hidden(true);
                            columns.Bound(p => p.Version);
                        })
           .Sortable()
           .Pageable()
           .DataSource(dataSource => dataSource
                                         .Ajax()
                                         .PageSize(7)
                                         .Sort(s => s.Add(c => c.Version).Descending())
                                         .Read(read => read.Action("GetSecondLevelData", "Results", new { productName = "#=ProductName#" }))
           )
           .ToClientTemplate()
      )    

</script>

@if (String.IsNullOrWhiteSpace(ViewBag.TestID))
{
@(Html.Kendo().Grid<ProductModel>()
        .Name("TopLevelGrid")
        .ClientDetailTemplateId("secondLevelTemplate")
        .Columns(columns => columns.Bound(p => p.ProductName).Title("Product"))
        .Sortable()
        .DataSource(dataSource => dataSource
                        .Ajax()
                        .Sort(s => s.Add(c => c.ProductName))
                        .Read(read => read.Action("GetFirstLevelData", "Results"))
        )
)
}
else
{
    @(Html.Kendo().Grid<Tests>()
           .Name("TestGrid")
           .ClientDetailTemplateId("testDetailsTemplate")
           .Columns(columns =>
           {
               columns.Bound(p => p.TestID).Hidden(true);
               columns.Bound(p => p.ProductName).Title("Product");
               columns.Bound(p => p.Version);
               columns.Bound(p => p.TestName);
               columns.Bound(p => p.StartTime).Format("{0:dd-MM-yyyy}");
               columns.Bound(p => p.EndTime).Format("{0:dd-MM-yyyy}");
               columns.Bound(p => p.Status);
               columns.Bound(p => p.MachineName).Title("Machine");
           })
           .Sortable()
           .Pageable(x => x.PageSizes(new[] { 10, 20, 50, 100, 500 }))
           .Scrollable()
           .Filterable()
           .DataSource(dataSource => dataSource
                        .Ajax()
                        .Sort(s => s.Add(c => c.StartTime))
                        .Read(read => read.Action("GetTestData", "Results"))
                        .PageSize(100)
           )
           .Events(ev => ev.DataBound("SetStatusColorDirect"))
    )
}
